//
//  MessageDetailViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/9/2.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MessageCarListViewController.h"
#import "MessageLineDetailCell.h"

#define SECTION_TIME_HEIGHT (33)
typedef NS_ENUM(NSInteger,Cell_Type) {
    Cell_Type_TimeL = 100,//时间cell上面的label
};
@interface MessageCarListViewController ()

@end

@implementation MessageCarListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"车辆消息";
    
    [self createTableView];
    
    [self.view addSubview:[self.viewModel returnNoDataView]];
    [self.viewModel reloadNoDataHidden:[self.viewModel MessageNums] > 0];
}

- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, 0 , __MAIN_WIDTH , __MAIN_HEIGHT_64);
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [Theme lineNewColor];
}
#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel MessageNums];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return ([[[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"type"] isEqualToString: @"date"] ? (SECTION_TIME_HEIGHT - SECTION_HEIGHT) : ([MessageLineDetailCell getCellHeightWithModel:[[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"info"]] + (indexPath.row == ([self.viewModel MessageNums] - 1) ? SECTION_HEIGHT : 0)));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    if ([[[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"type"] isEqualToString: @"model"]) {
        NSString * cellID = @"MessageCenterCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MessageLineDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        ((MessageLineDetailCell *)cell).topLineVHidden = [[[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"isFirst"] boolValue];
        ((MessageLineDetailCell *)cell).bottomLineVHidden = [[[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"isLast"] boolValue];
        
        __weak __typeof(self) wself = self;
        ((MessageLineDetailCell *)cell).clickLabelURL = ^(NSString *url)
        {
            [wself.viewModel clickLabelTextUrl:url];
        };
        [((MessageLineDetailCell *)cell) reloadDataWithModel:[[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"info"]];
    }else
    {
        NSString * cellID = @"DateCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.backgroundColor = [Theme backGroundColor];
            [[[UILabel labelWithFrame:CGRectMake(0, SECTION_HEIGHT, __MAIN_WIDTH, SECTION_TIME_HEIGHT - SECTION_HEIGHT * 2) font:FONT_DEFAULT_Light(12) text:@"" textColor:COLOR_HEXSTRING(@"#b0b0b0")] setupOnView:cell] labelResetTextAlignment:NSTextAlignmentCenter].tag = Cell_Type_TimeL;
        }
        UILabel * timeL = (UILabel *)[cell viewWithTag:Cell_Type_TimeL];
        timeL.text = [[self.viewModel carMessageAtIndex:indexPath.row] objectForKey:@"info"];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
